﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ServicesWatchDog
{
    public partial class frmAddService : Form
    {
        public ServiceDetails selectedServiceDetails = new ServiceDetails();
        List<ServiceDetails> servicesList = new List<ServiceDetails>();

        public frmAddService()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddService_Click(object sender, EventArgs e)
        {
            if (lstServices.SelectedIndex > -1)
            {
                string selectedServiceTitle = lstServices.SelectedItem.ToString();

                selectedServiceDetails = ServiceDetails.FindByTitle(selectedServiceTitle, servicesList);
            }
            this.Hide();
        }

        private void frmAddService_Activated(object sender, EventArgs e)
        {
            if (servicesList.Count <= 0)
            {
                progressIndicator.Visible = true;
                GetAllServices();
            }
        }

        private void GetAllServices()
        {
            ServiceController[] services = ServiceController.GetServices();

            if (services != null)
            {
                foreach (ServiceController service in services)
                {
                    ServiceDetails serviceDetails = new ServiceDetails { Name = service.ServiceName, Title = service.DisplayName };

                    servicesList.Add(serviceDetails);

                    lstServices.Items.Add(serviceDetails.Title);
                }
            }
        }
    }
}
